package com.mikufufu.blog.controller.admin;

import com.mikufufu.blog.model.entity.Nav;
import com.mikufufu.blog.model.vo.admin.NavVO;
import com.mikufufu.blog.service.NavService;
import com.mikufufu.blog.utils.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;
import com.mikufufu.blog.common.entity.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@RestController
@RequestMapping("/admin/nav")
@Api(tags = "导航接口")
public class NavAdminController {

    @Autowired
    private NavService navService;

    @GetMapping("/detail")
    @ApiOperation(value = "获取导航详情")
    @ApiImplicitParam(name = "id", value = "导航id", required = true)
    public  AjaxResult<Nav> detail(Integer id) {
        return AjaxResult.data(navService.getById(id));
    }

    @GetMapping("/getNavTree")
    @ApiOperation(value = "获取导航列表（树形数据）")
    public  AjaxResult<List<NavVO>> getNavTree() {
        return AjaxResult.data(navService.getNavTree());
    }

    @GetMapping("/getTreeByRoleId")
    @ApiOperation(value = "获取指定角色的导航列表（树形数据）")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true)
    public  AjaxResult<List<NavVO>> getTreeByRoleId(Integer roleId) {
        return AjaxResult.data(navService.getTreeByRoleId(roleId));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增导航")
    public AjaxResult<Boolean> add(@RequestBody Nav nav) {
        return AjaxResult.status(navService.save(nav));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改导航")
    public AjaxResult<Boolean> update(@RequestBody Nav nav) {
        return AjaxResult.status(navService.updateById(nav));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除导航")
    @ApiImplicitParam(name = "id", value = "导航id", required = true)
    public AjaxResult<Boolean> delete(Integer id) {
        return AjaxResult.status(navService.removeById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取导航列表")
    public AjaxResult<List<NavVO>> list() {
        return AjaxResult.data(BeanUtil.copyList(navService.list(new LambdaQueryWrapper<Nav>().eq(Nav::getStatus,0)), NavVO.class));
    }

    @GetMapping("/getNavList")
    @ApiOperation(value = "获取导航列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "navName", value = "导航名"),
            @ApiImplicitParam(name = "status", value = "状态值")
    })
    public AjaxResult<List<NavVO>> getNavList(String navName,Integer status) {
        return AjaxResult.data(navService.getNavList(navName,status));
    }

    @GetMapping("/listByParentId")
    @ApiOperation(value = "获取指定导航的子导航列表")
    @ApiImplicitParam(name = "parentId", value = "父导航id", required = true)
    public AjaxResult<List<NavVO>> listByParentId(Integer parentId) {
        return AjaxResult.data(navService.listByParentId(parentId));
    }

    @GetMapping("/listByParentIdAndStatus")
    @ApiOperation(value = "获取指定导航某个状态值的子列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父导航id", required = true),
            @ApiImplicitParam(name = "status", value = "状态值", required = true)
    })
    public AjaxResult<List<NavVO>> listByParentIdAndStatus(Integer parentId,Integer status) {
        return AjaxResult.data(navService.listByParentIdAndStatus(parentId,status));
    }

    @GetMapping("/addRoleNav")
    @ApiOperation(value = "给指定角色新增导航")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true),
            @ApiImplicitParam(name = "navId", value = "导航id", required = true)
    })
    public AjaxResult<Boolean> addRoleNav(Integer roleId, Integer navId) {
        return AjaxResult.status(navService.addRoleNav(roleId, navId));
    }

}
